package com.tencreat.mall.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencreat.common.annotation.Log;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.AjaxResult;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.common.enums.BusinessType;
import com.tencreat.common.utils.poi.ExcelUtil;
import com.tencreat.domain.mall.MallGoods;
import com.tencreat.domain.mall.MallStoreGoods;
import com.tencreat.domain.mall.dto.*;
import com.tencreat.domain.mall.vo.*;
import com.tencreat.service.mall.IMallGoodsService;
import com.tencreat.service.mall.IMallShopGoodsService;
import com.tencreat.service.mall.IMallStoreGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 店铺商品基本信息Controller
 *
 * @author tencreat
 * @date 2021-08-15
 */
@Api(tags="店铺-商品基本信息api 总后台管理-店铺商品相关api ")
@RestController
@RequestMapping("/mall/storeGoods")
public class AdminMallStoreGoodsController extends BaseController {

    @Autowired
//    private IMallGoodsService mallGoodsService;
    private IMallStoreGoodsService mallStoreGoodsService;



    @Autowired
    private IMallShopGoodsService mallShopGoodsService;

    /**
     * 查询店铺商品基本信息列表
     */
    @ApiOperation(value="查询店铺商品基本信息列表——店铺商品")
    @PreAuthorize("@ss.hasPermi('mall:store:list')")
    @PostMapping("/list")
    public HttpResult<IPage<MallStoreTableVO>> list(@RequestBody MallShopGoodsDTO mallShopGoodsDTO) {
        return HttpResult.ok(mallStoreGoodsService.pageTableVo(mallShopGoodsDTO));
    }

    /**
     * 导出店铺商品基本信息列表
     */
    @ApiOperation(value="导出店铺商品基本信息列表")
    @PreAuthorize("@ss.hasPermi('mall:store:export')")
    @Log(title = "总后台管理管理>商品管理>店铺商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(MallStoreGoods mallStoreGoods) {
        List<MallStoreGoods> list = mallStoreGoodsService.selectMallStoreGoodsList(mallStoreGoods);
        ExcelUtil<MallStoreGoods> util = new ExcelUtil<MallStoreGoods>(MallStoreGoods.class);
        return util.exportExcel(list, "goods");
    }

    /**
     * 获取店铺商品基本信息详细信息
     */
    @ApiOperation(value="获取店铺商品基本信息详细信息")
    @PreAuthorize("@ss.hasPermi('mall:store:query')")
    @PostMapping(value = "/getInfo")
    public HttpResult<MallStoreGoods> getInfo(@RequestBody   ReqIdParam id) {
        return HttpResult.ok(mallStoreGoodsService.selectMallStoreGoodsById(id));
    }

    /**
     * 新增店铺商品基本信息
     */
    @ApiOperation(value="新增店铺商品基本信息")
    @PreAuthorize("@ss.hasPermi('mall:store:add')")
    @Log(title = "店铺商品管理>店铺商品大厅", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public HttpResult<Boolean> add(@RequestBody MallShopGoodsAddDTO mallShopGoodsAddDTO) {
        if(mallShopGoodsAddDTO.getNorms().size()<1){
            return HttpResult.error("请上传规格！！");
        }
        return to(mallStoreGoodsService.insertMallStoreGoodsPo(mallShopGoodsAddDTO));
    }

    /**
     * 修改店铺商品基本信息
     */
    @ApiOperation(value="修改店铺商品基本信息")
    @PreAuthorize("@ss.hasPermi('mall:store:edit')")
    @Log(title = "店铺商品管理>店铺商品大厅", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public HttpResult<Boolean> edit(@RequestBody MallStoreGoods mallStoreGoods) {
        return to(mallStoreGoodsService.updateMallStoreGoods(mallStoreGoods));
    }

    /**
     * 删除店铺商品基本信息
     */
    @ApiOperation(value="删除店铺商品基本信息")
    @PreAuthorize("@ss.hasPermi('mall:store:remove')")
    @Log(title = "店铺商品管理>店铺商品大厅", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
    public HttpResult<Boolean> remove(@RequestBody  ReqIdsParam ids) {

        return to(mallStoreGoodsService.deleteMallStoreGoodsByIds(ids));
    }


    @ApiOperation(value = "获取店铺商品信息")
    @PreAuthorize("@ss.hasAnyPermi('mall:store:query,mall:store:edit')")
    @PostMapping("/getInfoGoodsVo")
    public HttpResult<MallStoreGoodsAllVO> getInfoGoodsVo(@RequestBody ReqIdParam id){
        return HttpResult.ok(mallStoreGoodsService.getInfoGoodsVo(id));
    }

    @ApiOperation(value = "更新店铺商品信息")
    @PreAuthorize("@ss.hasAnyPermi('mall:store:edit')")
    @Log(title = "店铺商品管理>店铺商品大厅", businessType = BusinessType.UPDATE)
    @PostMapping("/updateGoodsPo")
    public HttpResult<Boolean> updateGoodsPo(@RequestBody MallShopGoodsAddDTO mallShopGoodsAddDTO){
        return to(mallStoreGoodsService.updateMallStoreGoodsPo(mallShopGoodsAddDTO));
    }

    @ApiOperation(value = "最新店铺商品")
    @PostMapping("/newGoods")
    public HttpResult<List<MallStoreGoods>>getNewGoods(@RequestBody MallStoreGoods mallStoreGoods){
        return mallStoreGoodsService.selectNewGoods(mallStoreGoods);
    }


    /**
     * 修改店铺商品基本信息
     */
    @ApiOperation(value="批量修改店铺商品基本信息")
    @PreAuthorize("@ss.hasPermi('mall:store:editList')")
    @Log(title = "店铺商品管理>店铺商品大厅", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/editList")
    public HttpResult<Boolean> editList(@RequestBody  List<MallStoreGoods> mallStoreGoods) {
        int k=0;
        for(int i=0;i<mallStoreGoods.size();i++){
         k = mallStoreGoodsService.updateMallStoreGoods(mallStoreGoods.get(i));
        }
         return to(k);
    }

    @ApiOperation("店铺商品审核")
    @PreAuthorize("@ss.hasPermi('mall:store:audit')")
    @PostMapping("/audit")
    @Log(title = "店铺商品管理>店铺商品大厅", businessType = BusinessType.AUDIT)
    public HttpResult audit(@RequestBody @Validated MallGoodsAuditDTO dto) {
        mallStoreGoodsService.audit(dto);
        return HttpResult.ok();
    }

/*    @ApiOperation("供应商商品审核列表")
    @PreAuthorize("@ss.hasPermi('mall:goods:audit-supplier-list')")
    @PostMapping("/audit-supplier-list")
    HttpResult<Page<MallSupplierGoodsVO>> supplierList(@RequestBody @Validated MallSupplierGoodsAuditDTO dto) {
        dto.setAuditStatus(0);
        return HttpResult.ok(supplierGoodsService.listAudit(generatePage(dto), dto));
    }*/

    @ApiOperation("店铺商品审核列表")
    @PreAuthorize("@ss.hasPermi('mall:store:audit-store-list')")
    @PostMapping("/audit-store-list")
    HttpResult<Page<MallStoreGoodsVO>> storeList(@RequestBody @Validated MallStoreGoodsAuditDTO dto) {
        dto.setAuditStatus(0);
        return HttpResult.ok(mallShopGoodsService.listAudit(generatePage(dto), dto));
    }


    @ApiOperation("店铺商品数量统计")
    @GetMapping("/queryCount")
    public HttpResult queryCount() {
        return HttpResult.ok(mallStoreGoodsService.queryCount());
    }

    /*****
     *  将当前商品的排序减1 顺序上升一位
     * @param mallStoreGoods
     * @return
     */
    @PostMapping("/reSortOrderReduce")
    public HttpResult<Boolean> reSortOrderReduce(@RequestBody MallStoreGoods mallStoreGoods){
        return HttpResult.ok(mallStoreGoodsService.reSortOrderReduce(mallStoreGoods));
    }

    /*****
     * 将当前商品的排序加1 顺序下降一位
     * @param mallStoreGoods
     * @return
     */
    @PostMapping("/reSortOrderPlus")
    public HttpResult<Boolean> reSortOrderPlus(@RequestBody MallStoreGoods mallStoreGoods){
        return HttpResult.ok(mallStoreGoodsService.reSortOrderPlus(mallStoreGoods));
    }
}
